package com.offcn.itemweb.service.impl;

import com.alibaba.fastjson.JSON;
import com.offcn.common.entity.Result;
import com.offcn.itemweb.service.ItemwebService;
import com.offcn.sellergoods.feign.GoodsFeign;
import com.offcn.sellergoods.feign.ItemCatFeign;
import com.offcn.sellergoods.gruop.GoodsGroup;
import com.offcn.sellergoods.pojo.Goods;
import com.offcn.sellergoods.pojo.GoodsDesc;
import com.offcn.sellergoods.pojo.Item;
import com.offcn.sellergoods.pojo.ItemCat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;


import java.io.File;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ItemwebServiceimpl implements ItemwebService {

    @Autowired
    private GoodsFeign goodsFeign;

    @Autowired
    private ItemCatFeign itemCatFeign;

    @Value("${pagepath}")
    private String path;

    @Autowired
    private TemplateEngine templateEngine;
    /**
     * 生成html页面
     *
     * @param goodsId
     */
    @Override
    public void createHtml(Long goodsId) throws  Exception{
        //准备数据
        Map<String, Object> map = builderData(goodsId);
        Context context = new Context();
        context.setVariables(map);

        //指定生成文件写出路径
        File file = new File(path, goodsId + ".html");

        //使用字符流写出文件
        PrintWriter writer = new PrintWriter(file, "utf-8");

        //模板引擎生成文件
        templateEngine.process("item",context,writer);
    }

    private Map<String,Object> builderData(Long goodsId) {
        HashMap<String, Object> map = new HashMap<>();
        Result<GoodsGroup> result = goodsFeign.findById(goodsId);
        GoodsGroup goodsGroup = result.getData();
        Goods goods = goodsGroup.getGoods();
        GoodsDesc goodsDesc = goodsGroup.getGoodsDesc();
        List<Item> itemList = goodsGroup.getItemList();
        String specificationItems = goodsDesc.getSpecificationItems();
        List<Map> specificationList = JSON.parseArray(specificationItems, Map.class);
        String itemImages = goodsDesc.getItemImages();
        List<Map> imageList = JSON.parseArray(itemImages, Map.class);

        Long category1Id = goods.getCategory1Id();
        Result<ItemCat> result1 = itemCatFeign.findById(category1Id);
        String category1 = result1.getData().getName();

        Long category2Id = goods.getCategory2Id();
        Result<ItemCat> result2 = itemCatFeign.findById(category2Id);
        String category2 = result2.getData().getName();

        Long category3Id = goods.getCategory3Id();
        Result<ItemCat> result3 = itemCatFeign.findById(category3Id);
        String category3 = result3.getData().getName();

        map.put("goods",goods);
        map.put("goodsDesc",goodsDesc);
        map.put("itemList",itemList);
        map.put("specificationList",specificationList);
        map.put("imageList",imageList);
        map.put("category1",category1);
        map.put("category2",category2);
        map.put("category3",category3);
        return map;
    }
}
